<?php

/**
 * This is the model class for table "{{addresses}}".
 *
 * The followings are the available columns in table '{{addresses}}':
 * @property integer $id
 * @property integer $client_id
 * @property string $region
 * @property string $city
 * @property string $street
 * @property string $house
 * @property string $housing
 * @property string $apartment
 * @property integer $post_index
 * @property integer $type
 * @property string $created_at
 * @property string $updated_at
 * @property integer $active
 */
class Addresses extends CActiveRecord
{
	const TYPE_REGISTRATION = 1;
	const TYPE_HABITATION = 2;

	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Addresses the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{addresses}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('client_id, region, city, street, house,  apartment, post_index, created_at', 'required'),
			array('client_id, post_index, type, active', 'numerical', 'integerOnly'=>true),
			array('region, city, street, house, housing, apartment', 'length', 'max'=>255),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('client_id, id, region, city, street, house, housing, apartment, post_index, type, created_at, active', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	public function behaviors(){
		return array(
			'createUpdateBehavior' => array(
				'class' => 'application.behaviors.CreateDateBehavior',
				'createdField' => 'created_at'
			)
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'client_id' => 'Клиент',
			'region' => 'Регион',
			'city' => 'Город (населенный пункт)',
			'street' => 'Улица',
			'house' => 'Дом',
			'housing' => 'Корпус',
			'apartment' => 'Квартира',
			'post_index' => 'Почтовый индекс',
			'type' => 'Type',
			'created_at' => 'Created At',
			'updated_at' => 'Updated At',
			'active' => 'Active',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('region',$this->region,true);
		$criteria->compare('city',$this->city,true);
		$criteria->compare('street',$this->street,true);
		$criteria->compare('house',$this->house,true);
		$criteria->compare('housing',$this->housing,true);
		$criteria->compare('apartment',$this->apartment,true);
		$criteria->compare('post_index',$this->post_index);
		$criteria->compare('type',$this->type);
		$criteria->compare('created_at',$this->created_at,true);
		$criteria->compare('updated_at',$this->updated_at,true);
		$criteria->compare('active',$this->active);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
}